package com.whosly.strategy.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author yueny09 <deep_blue_yang@126.com>
 * @Date 2019-09-04 17:52
 */
public class LoggerUtil {
    /**
     * Logger available to subclasses.
     */
    private static final Map<String, Logger> loggerMap = new HashMap<>();
    private static Object obj = new Object();
    private static String DEFAULT_LOGGER_NAME = "SUPER-CLUB";

    public static Logger get(Class<?> clazz){
        if(clazz == null){
            return get(DEFAULT_LOGGER_NAME);
        }

        return get(clazz.getName());
    }

    private static Logger get(String clazzName){
        if(!loggerMap.containsKey(clazzName)){
            synchronized (obj){
                if(!loggerMap.containsKey(clazzName)){
                    if(StringUtils.isEmpty(clazzName)){
                        Logger logger = LoggerFactory.getLogger(LoggerUtil.class);
                        loggerMap.put(clazzName, logger);
                    }else{
                        Logger logger = LoggerFactory.getLogger(clazzName);
                        loggerMap.put(clazzName, logger);
                    }
                }
            }
        }

        return loggerMap.get(clazzName);
    }
}
